package com.example.demo.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/5/25.
 */
@Aspect
@Component
public class HttpAspect {
    private final static Logger logger= LoggerFactory.getLogger(HttpAspect.class);

//    @Pointcut("execution(public * com.example.demo.controller.AdminController.*(..))")
    @Pointcut("execution(public * com.example.demo.controller.*.*(..))")
    public void log(){
    }
    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
        logger.error("*****************************************");
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        //url
        logger.info("url={}",request.getRequestURI());
        //Method
        logger.info("Method={}",request.getMethod());
        //ip
        logger.info("IP={}",request.getRemoteAddr());
        //类方法
        logger.info("类方法={}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
        //参数
        logger.info("参数={}",joinPoint.getArgs());
        logger.info("请求={}",joinPoint.toString());

    }
    @After("log()")
    public void doAfter(){
        logger.error("*****************************************");
    }

    @AfterReturning(returning = "o",pointcut = "log()")
    public void doAfterReturning(Object o){
        logger.info("返回值={}",o);
        logger.error("*****************************************");
    }
}
